﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography; 

using edowork.com.SQL2005;
using edowork.com.Model;

namespace WebInvoicingsys.system.baseset.sysrole
{
    public partial class usermanagement_add : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        private void Reset()
        {
            txt_sysaccount.Text = "";
            txt_sysaccount.Focus();

            txt_syspassword.Text = "";
            txt_username.Text = "";
            txt_mobile.Text = "";
            txt_email.Text = "";
            txt_remark.Text = "";

            uc_from_stu.Reset();
            uc_from_department.Reset();
            uc_from_job.Reset();
            uc_from_rulegroup.Reset();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ;
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            alert_msg = "";

            edowork.com.Model.sysuserInfo sysuserInfo = new sysuserInfo();
            edowork.com.Model.sysuserruleInfo sysuserruleInfo = new sysuserruleInfo();

            sysuserInfo.Sysaccount = txt_sysaccount.Text.Trim();
            sysuserInfo.Syspassword = txt_syspassword.Text.Trim();
            sysuserInfo.Username = txt_username.Text.Trim();
            sysuserInfo.Mobile = txt_mobile.Text.Trim();
            sysuserInfo.Email = txt_email.Text.Trim();
            sysuserInfo.Stu1 = uc_from_stu.getValue();
            sysuserInfo.Remark = txt_remark.Text.Trim();
            sysuserInfo.Jobid = uc_from_job.getValue();
            sysuserInfo.Departmentid = uc_from_department.getValue();
            sysuserInfo.Sysdate = DateTime.Now.ToString("yyyy-MM-dd");

            sysuserruleInfo.Rulegroupid = uc_from_rulegroup.getValue();
            sysuserruleInfo.Sysdate = DateTime.Now.ToString("yyyy-MM-dd");

            if (String.IsNullOrEmpty(sysuserInfo.Sysaccount))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入登陆账号");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_sysaccount.Focus();

                return;
            }

            if (String.IsNullOrEmpty(sysuserInfo.Syspassword))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入登陆密码");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_syspassword.Focus();

                return;
            }

            if (String.IsNullOrEmpty(sysuserInfo.Username))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入用户名称");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_username.Focus();

                return;
            }

            if (String.IsNullOrEmpty(sysuserInfo.Stu1))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择启用状态");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(sysuserInfo.Departmentid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择所在部门");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(sysuserInfo.Jobid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择所任职务");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(sysuserruleInfo.Rulegroupid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择所属分组");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            //取得用户编码
            sysuserInfo.Userid = SysID.SCR_SysuserID(Session["connectionString"].ToString(), ref alert_msg);
            sysuserruleInfo.Userid = sysuserInfo.Userid;

            //保存数据入数据库
            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                //保存入sysuser表
                sql = String.Format(sysuserInfo.SQL_Insert(), sysuserInfo.Userid, sysuserInfo.Sysaccount, SysID.md532(sysuserInfo.Syspassword), sysuserInfo.Username, sysuserInfo.Mobile, sysuserInfo.Email, sysuserInfo.Stu1, sysuserInfo.Remark, sysuserInfo.Sysdate, sysuserInfo.Jobid, sysuserInfo.Departmentid);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                //保存入rulegroup表
                sql = String.Format(sysuserruleInfo.SQL_Insert(), sysuserruleInfo.Userid, sysuserruleInfo.Rulegroupid, sysuserruleInfo.Sysdate);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }

            Reset();

            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Save);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        //重置
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("/system/baseset/sysrole/usermanagement.aspx");
        }
    }
}